package com.lysh.aliHttp.utils;

import com.lysh.aliHttp.common.exception.ServiceException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyishan
 * @date 2023/5/5 09:17
 * @apiNote
 */

public class LogUtils {
    private static final Log log = LogFactory.getLog("com.lysh.aliHttp");
    private static List<String> errorCodeFilterList = new ArrayList();

    public LogUtils() {
    }

    public static Log getLog() {
        return log;
    }

    public static <ExType> void logException(String messagePrefix, ExType ex) {
        logException(messagePrefix, ex, true);
    }

    public static <ExType> void logException(String messagePrefix, ExType ex, boolean logEnabled) {
        assert ex instanceof Exception;

        String detailMessage = messagePrefix + ((Exception)ex).getMessage();
        if (ex instanceof ServiceException && errorCodeFilterList.contains(((ServiceException)ex).getErrorCode())) {
            if (logEnabled) {
                log.info(detailMessage);
            }
        } else if (logEnabled) {
            log.warn(detailMessage);
        }

    }

    static {
        errorCodeFilterList.add("NoSuchBucket");
        errorCodeFilterList.add("NoSuchKey");
        errorCodeFilterList.add("NoSuchUpload");
        errorCodeFilterList.add("NoSuchCORSConfiguration");
        errorCodeFilterList.add("NoSuchWebsiteConfiguration");
        errorCodeFilterList.add("NoSuchLifecycle");
    }
}
